#pragma once
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <strings.h>
#include <cstdlib>
#include <cstring>
#include "tcpLog.hpp"
#include "err.hpp"
class Sock
{
    const static int gback = 5;
    public:
        //创建套接字
        static int Socket()
        {
            int sock = socket(PF_INET, SOCK_STREAM, 0);
            if(sock < 0)
            {
                logMessage(FATAL, "create socket error");
                exit(SOCKET_ERR);
            }
            else
            {
                logMessage(NORMAL, "cteate socket success sock: %d", sock);
            }
            return sock;
        }

        //绑定
        static void Bind(int sock, uint16_t port)
        {
            struct sockaddr_in local;
            bzero(&local, sizeof(local));
            local.sin_family = PF_INET;
            local.sin_addr.s_addr = INADDR_ANY;
            local.sin_port = htons(port);

            int n = bind(sock, (struct sockaddr*)&local, sizeof(local));
            if(n < 0)
            {
                logMessage(FATAL, "bind error");
                exit(BIND_ERR);
            }
            else
            {
                logMessage(NORMAL, "bind success");
            }
        }

        //设置监听状态
        static void Listen(int sock)
        {
            int n = listen(sock, gback);  //设置全链接队列长度
            if(n < 0)
            {
                logMessage(FATAL, "listen error");
                exit(LISTEN_ERR);
            }
            else
            {
                logMessage(NORMAL, "listen success");
            }
        }

        //accept获取链接
        static int Accept(int listensock, std::string* clientip, uint16_t* clientport)
        {
            struct sockaddr_in client;
            socklen_t len = sizeof(client);
            int n = accept(listensock, (struct sockaddr *)&client, &len);
            if(n < 0)
            {
                logMessage(ERROR, "accept err");
            }
            else
            {
                logMessage(NORMAL, "accept a new link : %d", n);
                *clientip = inet_ntoa(client.sin_addr);
                *clientport = ntohs(client.sin_port);
            }
            return n;
        }
};